package com.knowledgepoint.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knowledgepoint.study.common.api.CommonPage;
import com.knowledgepoint.study.common.api.CommonResult;
import com.knowledgepoint.study.model.entity.KnowledgeType;
import com.knowledgepoint.study.service.KnowledgeTypeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识点类型管理
 */
@RestController
@RequestMapping("/knowledge/type")
public class KnowledgeTypeController {

    @Autowired
    private KnowledgeTypeService knowledgeTypeService;

    @PostMapping("/create")
    public CommonResult<Void> create(
            @ApiParam(value = "知识点类型信息", required = true)
            @Validated @RequestBody KnowledgeType knowledgeType) {
        if (knowledgeTypeService.isNameExist(knowledgeType.getName(), null)) {
            return CommonResult.failed("该类型名称已存在");
        }
        boolean success = knowledgeTypeService.save(knowledgeType);
        return success ? CommonResult.success(null, "创建成功") : CommonResult.failed("创建失败");
    }

    @ApiOperation(value = "修改知识点类型", notes = "根据ID修改知识点类型信息")
    @ApiResponses({
        @ApiResponse(code = 200, message = "操作成功"),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 404, message = "资源不存在"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @PostMapping("/update/{id}")
    public CommonResult<Void> update(
            @ApiParam(value = "知识点类型ID", required = true) @PathVariable Long id,
            @ApiParam(value = "知识点类型信息", required = true) @Validated @RequestBody KnowledgeType knowledgeType) {
        knowledgeType.setId(id);
        if (knowledgeTypeService.isNameExist(knowledgeType.getName(), id)) {
            return CommonResult.failed("该类型名称已存在");
        }
        boolean success = knowledgeTypeService.updateById(knowledgeType);
        return success ? CommonResult.success(null, "修改成功") : CommonResult.failed("修改失败");
    }

    @ApiOperation(value = "删除知识点类型", notes = "根据ID删除知识点类型")
    @ApiResponses({
        @ApiResponse(code = 200, message = "删除成功"),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 404, message = "资源不存在"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @PostMapping("/delete/{id}")
    public CommonResult<Void> delete(
            @ApiParam(value = "知识点类型ID", required = true) @PathVariable Long id) {
        // TODO: 检查该类型下是否有子类型，有则不允许删除
        boolean success = knowledgeTypeService.removeById(id);
        return success ? CommonResult.success(null, "删除成功") : CommonResult.failed("删除失败");
    }

    @ApiOperation(value = "分页查询知识点类型", notes = "根据关键字分页查询知识点类型列表")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @GetMapping("/list")
    public CommonResult<CommonPage<KnowledgeType>> list(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "keyword", required = false) String keyword,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", defaultValue = "10") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<KnowledgeType> page = knowledgeTypeService.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<KnowledgeType>()
                        .like(keyword != null, KnowledgeType::getName, keyword)
                        .or()
                        .like(keyword != null, KnowledgeType::getDescription, keyword)
                        .orderByAsc(KnowledgeType::getSort)
        );
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation(value = "获取所有启用的知识点类型", notes = "获取所有状态为启用的知识点类型列表")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @GetMapping("/list/enabled")
    public CommonResult<List<KnowledgeType>> listEnabled() {
        List<KnowledgeType> list = knowledgeTypeService.listEnabled();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "根据ID获取知识点类型详情", notes = "根据ID获取知识点类型的详细信息")
    @ApiResponses({
        @ApiResponse(code = 200, message = "查询成功"),
        @ApiResponse(code = 404, message = "资源不存在"),
        @ApiResponse(code = 500, message = "系统内部错误")
    })
    @GetMapping("/{id}")
    public CommonResult<KnowledgeType> getItem(
            @ApiParam(value = "知识点类型ID", required = true) @PathVariable Long id) {
        KnowledgeType knowledgeType = knowledgeTypeService.getById(id);
        return CommonResult.success(knowledgeType);
    }
}
